package com.rmdc.simop.commons.model.service.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.exception.ConstraintViolationException;

import com.rmdc.simop.commons.exception.GeneralException;
import com.rmdc.simop.commons.model.dao.BaseDAO;
import com.rmdc.simop.commons.model.service.BaseService;

public abstract class BaseServiceImpl<Entity extends Serializable> implements
		BaseService<Entity> {
	private static final Log log = LogFactory.getLog(BaseServiceImpl.class);

	protected String modulo;

	public BaseDAO<Entity> dao;

	protected Class<Entity> entityClass = getEntityClass();

	protected  abstract Class<Entity> getEntityClass();

	public BaseDAO<Entity> getDao() {
		return dao;
	}

	public void setDao(BaseDAO<Entity> dao) {
		this.dao = dao;
	}

	public String getModulo() {
		return modulo;
	}

	public void setModulo(String modulo) {
		this.modulo = modulo;
	}

	public List<Entity> findByCodigo(String codigo) throws GeneralException {
		log.info("buscando por " + entityClass.getName() + "S por Codigo:"
				+ codigo);
		if (codigo == null) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		List<Entity> l = null;
		try {
			l = dao.findByCodigo(codigo);
		} catch (org.springframework.dao.DataAccessResourceFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.exception.JDBCConnectionException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");

		} catch (Exception e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		if (l.size() == 0) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		return l;
	}



	public List<Entity> findByPropiedad(String valor, String nombrePropiedad)
			throws GeneralException {
		log.info("buscando " + entityClass.getSimpleName() + " por propiedad "
				+ nombrePropiedad + ":" + valor);
		if (valor == null || nombrePropiedad == null) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		List<Entity> l = null;
		try {
			l = dao.findByProperty(valor, nombrePropiedad);
		} catch (org.springframework.dao.DataAccessResourceFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.exception.JDBCConnectionException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");

		} catch (Exception e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		if (l.size() == 0) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		return l;
	}

	public Entity readByCodigo(String codigo) throws GeneralException {
		log.info("consultando " + entityClass.getSimpleName() + " por Codigo:"
				+ codigo);
		if (codigo == null) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		Entity entity = null;
		try {
			entity = dao.readByCodigo(codigo);
		} catch (org.springframework.dao.DataAccessResourceFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.exception.JDBCConnectionException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");

		} catch (Exception e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		if (entity == null) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		return entity;
	}

	public Entity readById(Integer id) throws GeneralException {
		log.info("buscando por " + entityClass.getSimpleName() + " por Id:" + id);
		if (id == null) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		Entity entity = null;
		try {
			entity = dao.readById(id);
		} catch (org.springframework.dao.DataAccessResourceFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.exception.JDBCConnectionException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");

		} catch (Exception e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		if (entity == null) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		return entity;

	}

	public List<Entity> findAll() throws GeneralException {
		log.info("consultarTodos los " + entityClass.getSimpleName());
		List<Entity> l = null;
		try {
			l = dao.findAll();
		} catch (org.springframework.dao.DataAccessResourceFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.exception.JDBCConnectionException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");

		} catch (Exception e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		if (l.size() == 0) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		return l;
	}

	public void delete(Entity object) throws GeneralException {
		log.info("Eliminando " + entityClass);
		if (object == null) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		try {
			dao.delete(object);
		} catch (org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.StaleStateException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
		} catch (org.springframework.dao.DataAccessResourceFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.exception.JDBCConnectionException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		} catch (org.springframework.dao.DataAccessException e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		} catch (Exception e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
	}

	public void deleteByCodigo(String codigo) throws GeneralException {
		log.info("eliminando por " + entityClass.getSimpleName() + " por Codigo:"
				+ codigo);
		if (codigo == null) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		try {
			dao.deleteByCodigo(codigo);
		} catch (org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.StaleStateException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
		} catch (org.springframework.dao.DataAccessResourceFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.exception.JDBCConnectionException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		} catch (org.springframework.dao.DataAccessException e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");

		} catch (java.lang.IllegalArgumentException e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");

		} catch (Exception e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
	}

	public void deleteById(Integer id) throws GeneralException {
		log
				.info("eliminando por " + entityClass.getSimpleName() + " por Id:"
						+ id);
		if (id == null) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		try {
			dao.deleteById(id);
		} catch (org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.StaleStateException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
		} catch (org.springframework.dao.DataAccessResourceFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.exception.JDBCConnectionException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		} catch (org.springframework.dao.DataAccessException e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");

		} catch (java.lang.IllegalArgumentException e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");

		} catch (Exception e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}

	}

	public void create(Entity object) throws GeneralException {
		log.info("incluyendo por " + entityClass.getSimpleName());
		if (object == null) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		try {
			dao.create(object);
		} catch (org.springframework.dao.DataIntegrityViolationException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.PropertyValueException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			} else if (e.getMostSpecificCause() instanceof ConstraintViolationException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");

		} catch (org.springframework.dao.DataAccessResourceFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.exception.JDBCConnectionException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		} catch (Exception e) {
			e.printStackTrace();
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
	}

	public void update(Entity object) throws GeneralException {
		log.info("modificando " + entityClass.getSimpleName());
		if (object == null) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
		try {
			dao.update(object);
		} catch (org.springframework.dao.DataIntegrityViolationException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.PropertyValueException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			} else if (e.getMostSpecificCause() instanceof ConstraintViolationException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		} catch (org.springframework.dao.DataAccessResourceFailureException e) {
			if (e.getMostSpecificCause() instanceof org.hibernate.exception.JDBCConnectionException) {
				throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
			}
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		} catch (org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException hofe) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}

		catch (Exception e) {
			throw new GeneralException("GENERAL_EXCEPTION_MESSAGES");
		}
	}

}
